<?php
	require_once PATH_OBJECT.'dataObject.php';
	
class dataObject_echeancier extends dataObject
{
 	/* @desc    Identifiant de l'échéance
     * @var     int
     */
    public $_IDENTIFIANT_ECH = 0;
    
/* @desc    Identifiant de l'échéance
     * @var     int
     */
    public $_DATE_DEBUT_ECH = '';
    
/* @desc    Identifiant de l'échéance
     * @var     int
     */
    public $_DATE_FIN_ECH = '';
    
/* @desc    Identifiant de l'échéance
     * @var     int
     */
    public $_ETAT_ECH = '';
    
/* @desc    Identifiant de l'échéance
     * @var     int
     */
    public $_MONTANT_ECH = 0;
    
/* @desc    Identifiant de l'échéance
     * @var     int
     */
    public $_IDENTIFIANT_COTISATION  = 0;
    
    
  public function __construct( $iId=0 )
    {
       parent::__construct();

       if ( $iId != 0 )
       {
            $sQuery = "SELECT   *   /*+ dataObject_echeancier::__construct */  
                       FROM echeancier 
            		   WHERE Identifiant_echeance = ".$iId;
            
            $aRows =  self::$_oConnexion->query($sQuery);
	  		$aRow = $aRows[0];
            $this->_IDENTIFIANT_ECH 	= $iId;
			$this->_DATE_DEBUT_ECH 		= self::showDate($aRow['Date_debut_echeance']);
			$this->_DATE_FIN_ECH 		= self::showDate($aRow['Date_fin_echeance']);
			$this->_ETAT_ECH 			= $aRow['Etat_echeance'];
			$this->_MONTANT_ECH			= $aRow['Montant_echeance'];
			$this->_IDENTIFIANT_COTISATION = $aRow['Identifiant_cotisation'];
	
       }
    }
    /**
     * récupere la table echeancier selon le(s) criter(s) précisés ou pas 
	 */
    function echeancier_select( $sCritere = null )
    {
        $aRow = false;
    	$sQuery = "SELECT   *    
                   FROM echeancier 
            	   WHERE 1=1 
            	   {$sCritere}";
        $aRow =  self::$_oConnexion->query($sQuery);

        return $aRow;
    }
//liste echeancier d'un contrat
public function echeancier_getListeEcheancierContrat($sCritere)
	{

        $aRow = false;
    	$sQuery = "SELECT   *    
                   FROM echeancier 
            	   WHERE Identifiant_cotisation="."'".$sCritere."'"
    				;
        $aRow =  self::$_oConnexion->query($sQuery);

        return $aRow;
   
	}

public function echeancier_insert()
    {	
    	$bId = false;
    	$sQuery = "INSERT /*echeancier_insert*/ INTO echeancier (
					Date_debut_echeance,
					Date_fin_echeance,
					Etat_echeance,
					Montant_echeance,
					Identifiant_cotisation
    				) values (
	    			".self::$_oConnexion->date($this->_DATE_DEBUT_ECH).",
	    			".self::$_oConnexion->date($this->_DATE_FIN_ECH).",
	    			'$this->_ETAT_ECH',
	    			'$this->_MONTANT_ECH',
	    			$this->_IDENTIFIANT_COTISATION
	    			)";
    
        	$bId = self::$_oConnexion->execute($sQuery);
    	return $bId;
    }
    
    public function echeancier_update()
    {    	
    	$sQuery = "UPDATE echeancier SET 
    		 	xxxx_nom 		= '$this->_XXXX' ,         
	            xxxx_		= '$this->_XXXX' ,   
	            xxxx_ 	=  $this->_XXXX ,
	            xxxx_ 	=  $this->_XXXX ,    
	            xxxx_ 	= '$this->_XXXX' ,
	            xxxx_ 		= '$this->_XXXX' ,   
	            xxxx_ 		= '$this->_XXXX',
	            xxxx_ 		=  $this->_XXXX   
	    		WHERE Identifiant_echeance 	= ".$this->_IDENTIFIANT_ECH;
    	
    	return self::$_oConnexion->execute($sQuery);
    }
    /******************************** Mamadou ***************************/
    function echeance_verifMontant($sMttEch)
    {
        $aRow = false;
    	$sQuery = "SELECT  *    
                   FROM echeancier 
            	   WHERE Identifiant_echeance = '".$this->_IDENTIFIANT_ECH."' AND Montant_echeance = '".$sMttEch."'";
        $aRow =  self::$_oConnexion->query($sQuery);
		
        if(!empty($aRow)) 
        	return true;
        else return false;
    }
	/*
     * Vérifie, à partir de son Id, si une échéance est appelée ou impayée
     */
    public function echeance_verifEtat() //$sIdEch)
    {
        $aRow = false;
    
	    	$sCritere = "SELECT *  
	    				FROM echeancier 
	    				WHERE (Etat_echeance = 'I' OR Etat_echeance = 'A')
	    				AND Identifiant_echeance = ".$this->_IDENTIFIANT_ECH; //$sIdEch;
	        $aRow =  self::$_oConnexion->query($sCritere);

	    if(!empty($aRow)) 
        	return true;
        else return false;

    }
   
    /*
     *  Vérifie si montant règlement égal au montant échéance
     */
	public function echeance_reglEtat()
    {    	
    	$sQuery = "UPDATE echeancier SET     		 	
	            Etat_echeance 	=  'R' 
	    		WHERE Identifiant_echeance 	= ".$this->_IDENTIFIANT_ECH;
    	
    	return self::$_oConnexion->execute($sQuery);
    }
	/*
     * Recherche le numéro de cotisation d'une échéance
     */
    function echeance_searchCotisation()
    {
        $aRow = false;
    	$sQuery = "SELECT  Identifiant_cotisation    
                   FROM echeancier 
            	   WHERE Identifiant_echeance= ".$this->_IDENTIFIANT_ECH;
        $aRow =  self::$_oConnexion->query($sQuery);
		
        return $aRow[0]['Identifiant_cotisation'];
    }
    
	/*
     * Récupère le numéro du contrat correspondant à l'échéance
     */
    public function echeance_getIdContrat()
    {
         $aRow = false;
    	$sCritere = "SELECT Identifiant_contrat
    				FROM cotisation  
    				WHERE Identifiant_cotisation = ".$this->_IDENTIFIANT_COTISATION;
        $aRow =  self::$_oConnexion->query($sCritere);
		
        return $aRow[0]['Identifiant_contrat'];
    }
	
	/************************* FIN  Mamadou *************************************/
    
    public function getEcheanceRegleCotis($dDateDebut, $dDateFin, $mMntEcheance, $nIdCotis)
    {
    	$bIdE = false;
    	$sReqE = "SELECT * 
    			FROM echeancier 
    			WHERE Date_debut_echeance='".$dDateDebut."' AND 
    				Date_fin_echeance='".$dDateFin."' AND 
    				Etat_echeance='R' AND 
    				Montant_echeance = '".$mMntEcheance." AND 
    				Identifiant_cotisation='".$nIdCotis."'";
    	
    	$aRow =  self::$_oConnexion->query($sReqE);
    	
    	return $sReqE[0];
    }
    
	public function echeancier_getEcheanceRegleForContrat($IdContrat)
    {

    	$sQuery = "SELECT Identifiant_echeance
    			 FROM echeancier ech, cotisation cot
    			WHERE Etat_echeance = 'R' 
    			  AND ech.Identifiant_cotisation = cot.Identifiant_cotisation
    			   AND  cot.Identifiant_contrat = ".$IdContrat;
    	
    	$aRow =  self::$_oConnexion->query($sQuery);
    	
    	if(!empty($aRow))
    	return $aRow[0]['Identifiant_echeance'];
    	else return 0;
    }
    
	public function getEcheanceAAppelerCotis($dDateDebut,$dDateFin,$mMntEcheance,$nIdCotis){
    	$bIdE = false;
    	$sReqE = "SELECT * 
    			FROM echeancier 
    			WHERE Date_debut_echeance='".$dDateDebut."' AND 
    				Date_fin_echeance='".$dDateFin."' AND 
    				Etat_echeance='AA' AND 
    				Montant_echeance = '".$mMntEcheance." AND 
    				Identifiant_cotisation='".$nIdCotis."'";
    	
    	$aRow =  self::$_oConnexion->query($sReqE);
    	
    	return $sReqE[0];
    }
}
?>